package
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;

	public class ToolTipController extends MovieClip
	{
		private var _tf:TextField;
		private var _timer:Timer;

		private var level0:Sprite=new Sprite();
		private var level1:Sprite=new Sprite();
		private var level2:Sprite=new Sprite();
		private static var _instance:ToolTipController;

		public static function instance():ToolTipController
		{
			if(_instance==null) _instance=new ToolTipController();
			
			return _instance;
		}

		public function Init():void
		{
			level0.x=500;
			level1.x=300;
			
			this.addChild(level0);
			this.addChild(level1);
			this.addChild(level2);
			
			level0.graphics.beginFill(0x123456);
			level0.graphics.drawCircle(100, 100, 20);
			level0.graphics.endFill();

			level1.graphics.beginFill(0x654321);
			level1.graphics.drawCircle(100, 100, 20);
			level1.graphics.endFill();

			level2.graphics.beginFill(0x123654);
			level2.graphics.drawCircle(100, 100, 20);
			level2.graphics.endFill();

			level1.buttonMode=true;
			level2.buttonMode=true;
			level0.buttonMode=true;

			
			
			level0.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOverZero);
			level1.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOver);
			level2.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOverTwo);

			this.debug();
		}

		private function onMouseOver(event:MouseEvent):void
		{
			var tf:TextFormat=new TextFormat();
			tf.bold=true;
			tf.size=12;
			tf.color=0xff0000;
			var tt:ToolTip=new ToolTip();
			tt.hook=true;
			tt.cornerRadius=20;
			tt.autoSize=true;
			tt.align="right";
			tt.show(level1, "Simple ToolTip", "变变");
			//this.traceDisplayList( stage );
		}
		private function onMouseOverZero(event:MouseEvent):void
		{
			var tf:TextFormat=new TextFormat();
			tf.font="georgia"; //font in library
			tf.bold=true;
			tf.size=20;
			tf.color=0x01430E;

			var contentFormat:TextFormat=new TextFormat();
			contentFormat.size=14;
			contentFormat.color=0xFFFFFF;
			contentFormat.bold=false;

			var tt:ToolTip=new ToolTip();
			tt.tipWidth=250;
			tt.delay=1000;
			tt.titleFormat=tf;
			tt.contentFormat=contentFormat;
			tt.align="left";
			tt.colors=[0xB5FEB4, 0x003300];
			tt.show(level0, "Styled Tip", "ok");
		}

		private function onMouseOverTwo(event:MouseEvent):void
		{
			var tt:ToolTip=new ToolTip();
			tt.align="center";
			tt.hook=true;
			tt.cornerRadius=0;
			tt.show(level2, "Simple Tip", "中间");
		}

		private function debug():void
		{
			/* Memory Testing */
			_tf=new TextField();
			addChild(_tf);
			this._timer=new Timer(25);
			this._timer.addEventListener("timer", onTimer);
			this._timer.start();
		}

		private function onTimer(event:TimerEvent):void
		{
			_tf.text=flash.system.System.totalMemory.toString();
		}

		protected function traceDisplayList(container:DisplayObjectContainer, indentString:String=""):void
		{
			var child:DisplayObject;
			for (var i:uint=0; i < container.numChildren; i++)
			{
				child=container.getChildAt(i);
				trace(indentString, child, child.name);
				if (container.getChildAt(i) is DisplayObjectContainer)
				{
					traceDisplayList(DisplayObjectContainer(child), indentString + "    ");
				}
			}
		}
	}
}
